const { DataTypes } = require("sequelize");
const { sequelize } = require("../dbConnect");

// 功能模块表
const FunctionModule = sequelize.define('function_modules', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "功能模块ID"
  },
  key: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    comment: "功能键值"
  },
  name: {
    type: DataTypes.STRING(100),
    allowNull: false,
    comment: "功能名称"
  },
  icon: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: "图标"
  },
  bg_color: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "背景色"
  },
  icon_color: {
    type: DataTypes.STRING(20),
    allowNull: true,
    comment: "图标颜色"
  },
  route: {
    type: DataTypes.STRING(200),
    allowNull: true,
    comment: "路由地址"
  },
  sort: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "排序"
  },
  enabled: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  }
}, {
  tableName: 'function_modules',
  indexes: [
    {
      unique: true,
      fields: ['key']
    }
  ]
});

// 通知消息表
const Notification = sequelize.define('notifications', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "通知ID"
  },
  user_id: {
    type: DataTypes.INTEGER,
    allowNull: false,
    comment: "用户ID"
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "通知标题"
  },
  content: {
    type: DataTypes.TEXT,
    allowNull: false,
    comment: "通知内容"
  },
  type: {
    type: DataTypes.ENUM('system', 'course', 'activity', 'achievement'),
    defaultValue: 'system',
    comment: "通知类型"
  },
  status: {
    type: DataTypes.ENUM('read', 'unread'),
    defaultValue: 'unread',
    comment: "阅读状态"
  },
  extra: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "额外数据"
  },
  read_at: {
    type: DataTypes.DATE,
    allowNull: true,
    comment: "阅读时间"
  }
}, {
  tableName: 'notifications',
  indexes: [
    {
      fields: ['user_id']
    },
    {
      fields: ['user_id', 'status']
    }
  ]
});

// 推荐内容表
const Recommendation = sequelize.define('recommendations', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    comment: "推荐内容ID"
  },
  title: {
    type: DataTypes.STRING(200),
    allowNull: false,
    comment: "标题"
  },
  description: {
    type: DataTypes.TEXT,
    allowNull: true,
    comment: "描述"
  },
  cover: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "封面图片URL"
  },
  rating: {
    type: DataTypes.DECIMAL(3, 2),
    defaultValue: 0.00,
    comment: "评分"
  },
  study_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "学习人数"
  },
  type: {
    type: DataTypes.ENUM('course', 'article', 'video', 'audio'),
    allowNull: false,
    comment: "内容类型"
  },
  duration: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "时长(秒)"
  },
  difficulty: {
    type: DataTypes.ENUM('beginner', 'intermediate', 'advanced'),
    defaultValue: 'beginner',
    comment: "难度级别"
  },
  category_id: {
    type: DataTypes.INTEGER,
    allowNull: true,
    comment: "分类ID"
  },
  instructor: {
    type: DataTypes.STRING(100),
    allowNull: true,
    comment: "讲师"
  },
  price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.00,
    comment: "价格"
  },
  original_price: {
    type: DataTypes.DECIMAL(10, 2),
    defaultValue: 0.00,
    comment: "原价"
  },
  tags: {
    type: DataTypes.JSON,
    allowNull: true,
    comment: "标签"
  },
  content_url: {
    type: DataTypes.STRING(500),
    allowNull: true,
    comment: "内容链接"
  },
  sort: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "排序"
  },
  is_featured: {
    type: DataTypes.BOOLEAN,
    defaultValue: false,
    comment: "是否精选"
  },
  is_active: {
    type: DataTypes.BOOLEAN,
    defaultValue: true,
    comment: "是否启用"
  },
  view_count: {
    type: DataTypes.INTEGER,
    defaultValue: 0,
    comment: "查看次数"
  }
}, {
  tableName: 'recommendations',
  indexes: [
    {
      fields: ['category_id']
    },
    {
      fields: ['type', 'is_active']
    }
  ]
});

module.exports = {
  FunctionModule,
  Notification,
  Recommendation
};
